package com.moon.game;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

/**
 * @description 测试mongo
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-26 下午10:01:58
 */
public class Test {
	
	public static void main(String[] args) throws MongoException, UnknownHostException, InvalidProtocolBufferException {
		List<DBObject> datas = new Test().findAll();
		for (DBObject data:datas) {
//			Player player = Player.parseFrom(ByteString.copyFrom((byte[])(data.get("data"))));
//			System.out.println(player.getName());
			System.out.println(data);
		}
	}
	public List<DBObject> findAll() throws MongoException, UnknownHostException {
		MongoOptions options = new MongoOptions();
		options.autoConnectRetry = true;
		Mongo mongo = new Mongo(new ServerAddress("127.0.0.1", 27017), options);
		DB db = mongo.getDB("player");
		DBCollection dbcoll = db.getCollection("player");
		DBCursor cur = dbcoll.find();
		List<DBObject> list = new ArrayList<DBObject>();
		if (cur != null) {
			list = cur.toArray();
		}

		return list;
	}

}
